var bingProperties = require('./bingProperties');
var soap = require('./soap');
var SOAP = bingProperties.SOAP;
var ET = bingProperties.ET;
var events = require('events').EventEmitter;

var PollReportRequest = function() {
        this.root = ET.Element('{' + SOAP.ns_soapenv + '}Envelope');
        this.root.set('xsi', SOAP.ns_xsi);
        this.header = ET.SubElement(this.root, '{' + SOAP.ns_soapenv + '}Header');
        this.header.set('xmlns', SOAP.ns_bingads);
        this.body = ET.SubElement(this.root, '{' + SOAP.ns_soapenv + '}Body');
        this.pollGenerateReportRequest = ET.SubElement(this.body, 'PollGenerateReportRequest');
        this.reportRequestId = ET.Element('ReportRequestId');
        this.pollGenerateReportRequest.set('xmlns', SOAP.ns_bingads);
        this.pollGenerateReportRequest.append(this.reportRequestId);
    };
PollReportRequest.prototype.toXMLString = function() {
    // console.log('toXMLString');
    return toXMLString.call(this.root);
};
PollReportRequest.prototype.setReportRequestId = function(reportRequestId) {
    this.reportRequestId.text = reportRequestId;
};
PollReportRequest.prototype.getReportRequestId = function(reportRequestId) {
    return this.reportRequestId;
};
PollReportRequest.prototype.setApplicationToken = function(applicationToken) {
    this.applicationToken = ET.SubElement(this.header, 'ApplicationToken');
    this.applicationToken.text = applicationToken;
};
PollReportRequest.prototype.getApplicationToken = function() {
    return this.applicationToken;
};
PollReportRequest.prototype.setCustomerAccountId = function(customerAccountId) {
    this.customerAccountId = ET.SubElement(this.header, 'CustomerAccountId');
    this.customerAccountId.text = customerAccountId;
};
PollReportRequest.prototype.getCustomerAccountId = function() {
    return this.customerAccountId;
};
PollReportRequest.prototype.setCustomerId = function(customerId) {
    this.customerId = ET.SubElement(this.header, 'CustomerId');
    this.customerId.text = customerId;
};
PollReportRequest.prototype.getCustomerId = function() {
    return this.customerId;
};
PollReportRequest.prototype.setDeveloperToken = function(developerToken) {
    this.developerToken = ET.SubElement(this.header, 'DeveloperToken');
    this.developerToken.text = developerToken;
};
PollReportRequest.prototype.getDeveloperToken = function() {
    return this.developerToken;
};
PollReportRequest.prototype.setPassword = function(password) {
    this.password = ET.SubElement(this.header, 'Password');
    this.password.text = password;
};
PollReportRequest.prototype.getPassword = function() {
    return this.password;
};
PollReportRequest.prototype.setUserName = function(userName) {
    this.userName = ET.SubElement(this.header, 'UserName');
    this.userName.text = userName;
};
PollReportRequest.prototype.getUserName = function() {
    return this.userName;
};
PollReportRequest.prototype.setAction = function(action) {
    this.action = ET.SubElement(this.header, 'Action');
    this.action.set('mustUnderstand', 1);
    this.action.text = action;
};
PollReportRequest.prototype.getAction = function(action) {
    return this.action;
};



var ReportRequest = function() {
        this.root = ET.Element('{' + SOAP.ns_soapenv + '}Envelope');
        this.root.set('xmlns:xsi', SOAP.ns_xsi);
        this.header = ET.SubElement(this.root, '{' + SOAP.ns_soapenv + '}Header');
        this.header.set('xmlns', SOAP.ns_bingads);
        this.body = ET.SubElement(this.root, '{' + SOAP.ns_soapenv + '}Body');
        this.submitGenerateReportRequest = ET.SubElement(this.body, 'SubmitGenerateReportRequest');
        this.submitGenerateReportRequest.set('xmlns', SOAP.ns_bingads);
        // self.emit('reportData');self.
        // console.log(self.root);
    };
ReportRequest.prototype = Object.create(events.prototype, {
    constructor: {
        value: function() {
            events.call(this);
        },
        enumerable: false
    }
});

ReportRequest.prototype.createAdExtensionByAdsReportRequest = function() {
    var rst = {};
    var self = this;
    this.reportType = 'adExtensionByAdsReportRequest';
    this.adExtensionByAdsReportRequest = ET.Element('ReportRequest');
    this.adExtensionByAdsReportRequest.set('xsi:type', 'AdExtensionByAdsReportRequest');
    this.submitGenerateReportRequest.append(this.adExtensionByAdsReportRequest);

    rst.target = this.adExtensionByAdsReportRequest;
    rst.setFormat = function() {
        self[self.reportType].append(self.reportFormat);
    };
    rst.setLanguage = function() {
        self[self.reportType].append(self.reportLanguage);
    };
    rst.setReportName = function() {
        self[self.reportType].append(self.reportName);
    };
    rst.setReturnOnlyCompleteData = function(returnOnlyCompleteData) {

        self[self.reportType].append(returnOnlyCompleteData);

    };
    rst.setAggregation = function(aggregation) {
        self.aggregation = ET.Element('Aggregation');
        self.aggregation.text = aggregation;
        self[self.reportType].append(self.aggregation);
    };
    rst.setColumns = function(columns) {
        self[self.reportType].append(columns);
    };
    rst.setScope = function(scope) {
        self[self.reportType].append(scope);
    };
    rst.setTime = function(time) {
        self[self.reportType].append(time);
    };
    return rst;
};
ReportRequest.prototype.createAdExtensionByAdsReportColumn = function() {
    var rst = {};
    var self = this;
    this.columns = ET.Element('Columns');
    rst.target = this.columns;
    rst.addColumn = function(colmunName) {
        var column = ET.Element('{' + SOAP.ns_bingads + '}AdExtensionByAdsReportColumn');
        // var column = ET.Element('KeywordPerformanceReportColumn');
        column.text = colmunName;
        self.columns.append(column);
        // rst.columns = self.columns;
    };
    rst.setAdDistribution = function(adDistribution) {
        self.columns.append(adDistribution);
    }
    return rst;
};
ReportRequest.prototype.createAdDynamicTextPerformanceReportRequest = function() {
    var rst = {};
    var self = this;
    this.reportType = 'adDynamicTextPerformanceReportRequest';
    this.adDynamicTextPerformanceReportRequest = ET.Element('ReportRequest');
    this.adDynamicTextPerformanceReportRequest.set('xsi:type', 'AdDynamicTextPerformanceReportRequest');
    this.submitGenerateReportRequest.append(this.adDynamicTextPerformanceReportRequest);

    rst.target = this.adDynamicTextPerformanceReportRequest;
    rst.setFormat = function() {
        self[self.reportType].append(self.reportFormat);
    };
    rst.setLanguage = function() {
        self[self.reportType].append(self.reportLanguage);
    };
    rst.setReportName = function() {
        self[self.reportType].append(self.reportName);
    };
    rst.setReturnOnlyCompleteData = function(returnOnlyCompleteData) {

        self[self.reportType].append(returnOnlyCompleteData);

    };
    rst.setAggregation = function(aggregation) {
        self.aggregation = ET.Element('Aggregation');
        self.aggregation.text = aggregation;
        self[self.reportType].append(self.aggregation);
    };
    rst.setColumns = function(columns) {
        self[self.reportType].append(columns);
    };
    rst.setScope = function(scope) {
        self[self.reportType].append(scope);
    };
    rst.setTime = function(time) {
        self[self.reportType].append(time);
    };
    return rst;
};
ReportRequest.prototype.createAdDynamicTextPerformanceReportColumn = function() {
    var rst = {};
    var self = this;
    this.columns = ET.Element('Columns');
    rst.target = this.columns;
    rst.addColumn = function(colmunName) {
        var column = ET.Element('{' + SOAP.ns_bingads + '}AdDynamicTextPerformanceReportColumn');
        // var column = ET.Element('KeywordPerformanceReportColumn');
        column.text = colmunName;
        self.columns.append(column);
        // rst.columns = self.columns;
    };
    rst.setAdDistribution = function(adDistribution) {
        self.columns.append(adDistribution);
    }
    return rst;
};
ReportRequest.prototype.createCampaignPerformanceReportRequest = function() {
    var rst = {};
    var self = this;
    this.reportType = 'campaignPerformanceReportRequest';
    this.campaignPerformanceReportRequest = ET.Element('ReportRequest');
    this.campaignPerformanceReportRequest.set('xsi:type', 'CampaignPerformanceReportRequest');
    this.submitGenerateReportRequest.append(this.campaignPerformanceReportRequest);

    rst.target = this.campaignPerformanceReportRequest;
    rst.setFormat = function() {
        self[self.reportType].append(self.reportFormat);
    };
    rst.setLanguage = function() {
        self[self.reportType].append(self.reportLanguage);
    };
    rst.setReportName = function() {
        self[self.reportType].append(self.reportName);
    };
    rst.setReturnOnlyCompleteData = function(returnOnlyCompleteData) {

        self[self.reportType].append(returnOnlyCompleteData);

    };
    rst.setAggregation = function(aggregation) {
        self.aggregation = ET.Element('Aggregation');
        self.aggregation.text = aggregation;
        self[self.reportType].append(self.aggregation);
    };
    rst.setColumns = function(columns) {
        self[self.reportType].append(columns);
    };
    rst.setScope = function(scope) {
        self[self.reportType].append(scope);
    };
    rst.setTime = function(time) {
        self[self.reportType].append(time);
    };
    return rst;
};
ReportRequest.prototype.createCampaignPerformanceReportColumn = function() {
    var rst = {};
    var self = this;
    this.columns = ET.Element('Columns');
    rst.target = this.columns;
    rst.addColumn = function(colmunName) {
        var column = ET.Element('{' + SOAP.ns_bingads + '}CampaignPerformanceReportColumn');
        // var column = ET.Element('KeywordPerformanceReportColumn');
        column.text = colmunName;
        self.columns.append(column);
        // rst.columns = self.columns;
    };
    rst.setAdDistribution = function(adDistribution) {
        self.columns.append(adDistribution);
    }
    return rst;
};
ReportRequest.prototype.createAccountPerformanceReportColumn = function() {
    var rst = {};
    var self = this;
    this.columns = ET.Element('Columns');
    rst.target = this.columns;
    rst.addColumn = function(colmunName) {
        var column = ET.Element('{' + SOAP.ns_bingads + '}AccountPerformanceReportColumn');
        // var column = ET.Element('KeywordPerformanceReportColumn');
        column.text = colmunName;
        self.columns.append(column);
        // rst.columns = self.columns;
    };
    rst.setAdDistribution = function(adDistribution) {
        self.columns.append(adDistribution);
    }
    return rst;
};
ReportRequest.prototype.createAccountPerformanceReportRequest = function() {
    var rst = {};
    var self = this;
    this.reportType = 'accountPerformanceReportRequest';
    this.accountPerformanceReportRequest = ET.Element('ReportRequest');
    this.accountPerformanceReportRequest.set('xsi:type', 'AccountPerformanceReportRequest');
    this.submitGenerateReportRequest.append(this.accountPerformanceReportRequest);

    rst.target = this.accountPerformanceReportRequest;
    rst.setFormat = function() {
        self[self.reportType].append(self.reportFormat);
    };
    rst.setLanguage = function() {
        self[self.reportType].append(self.reportLanguage);
    };
    rst.setReportName = function() {
        self[self.reportType].append(self.reportName);
    };
    rst.setReturnOnlyCompleteData = function(returnOnlyCompleteData) {

        self[self.reportType].append(returnOnlyCompleteData);

    };
    rst.setAggregation = function(aggregation) {
        self.aggregation = ET.Element('Aggregation');
        self.aggregation.text = aggregation;
        self[self.reportType].append(self.aggregation);
    };
    rst.setColumns = function(columns) {
        self[self.reportType].append(columns);
    };
    rst.setScope = function(scope) {
        self[self.reportType].append(scope);
    };
    rst.setTime = function(time) {
        self[self.reportType].append(time);
    };
    return rst;
};
ReportRequest.prototype.createAdDistribution = function(adDistribution) {
    var rst = {};
    this.adDistribution = ET.Element('AdDistributionReportFilter');
    this.adDistribution.text = adDistribution;
    rst.target = this.adDistribution;
    return rst;
}
// ReportRequest.prototype.set
ReportRequest.prototype.setAction = function(action) {
    this.action = ET.SubElement(this.header, 'Action');
    this.action.set('mustUnderstand', 1);
    this.action.text = action;
};
ReportRequest.prototype.getAction = function(action) {
    return this.action;
};


ReportRequest.prototype.createTime = function(time) {
    var rst = {};
    var self = this;
    this.time = ET.Element('Time');
    rst.target = this.time;
    rst.setPredefinedTime = function(preTime) {
        var predefindTime = ET.Element('PredefinedTime');
        predefindTime.text = preTime;
        self.time.append(predefindTime);
    };
    rst.setCustomDateRangeStart = function(date) { //YYYY-MM-DD
        if(self.customDateRangeStart) {
            self.time.remove(self.customDateRangeStart);
        }
        var times = date.split('-');
        // console.log('setCustomDateRangeStart',times,times[2]);
        self.customDateRangeStart = ET.Element('CustomDateRangeStart');
        var day = ET.SubElement(self.customDateRangeStart, 'Day');
        day.text = times[2];
        // console.log('setCustomDateRangeStart',times,times[2],day.text);
        var month = ET.SubElement(self.customDateRangeStart, 'Month');
        month.text = times[1];
        var year = ET.SubElement(self.customDateRangeStart, 'Year');
        year.text = times[0];
        self.time.append(self.customDateRangeStart);
    }
    rst.setCustomDateRangeEnd = function(date) { //YYYY-MM-DD
        // console.log('setCustomDateRangeEnd',date);
        if(self.customDateRangeEnd) {
            self.time.remove(self.customDateRangeEnd);
        }
        var times = date.split('-');
        self.customDateRangeEnd = ET.Element('CustomDateRangeEnd');
        var day = ET.SubElement(self.customDateRangeEnd, 'Day');
        day.text = times[2];
        var month = ET.SubElement(self.customDateRangeEnd, 'Month');
        month.text = times[1];
        var year = ET.SubElement(self.customDateRangeEnd, 'Year');
        year.text = times[0];
        self.time.append(self.customDateRangeEnd);
    }
    return rst;
    // self[self.reportType].append(scope);
};


ReportRequest.prototype.createAccountIds = function() {
    var rst = {};
    var self = this;
    this.accountIds = ET.Element('AccountIds');
    rst.target = this.accountIds;
    rst.addAccountId = function(accId) {
        var accountId = ET.Element('{' + SOAP.ns_arrays + '}long');
        accountId.text = accId;
        self.accountIds.append(accountId);
    };
    return rst;
};
ReportRequest.prototype.createScope = function() {
    var rst = {};
    var self = this;
    this.scope = ET.Element('Scope');
    rst.target = this.scope;
    rst.setAccountIds = function(accountIds) {
        self.scope.append(accountIds);
    };
    return rst;
};


ReportRequest.prototype.createKeywordPerformanceReportColumn = function() {
    var rst = {};
    var self = this;
    this.columns = ET.Element('Columns');
    rst.target = this.columns;
    rst.addColumn = function(colmunName) {
        var column = ET.Element('{' + SOAP.ns_bingads + '}KeywordPerformanceReportColumn');
        // var column = ET.Element('KeywordPerformanceReportColumn');
        column.text = colmunName;
        self.columns.append(column);
        // rst.columns = self.columns;
    };
    rst.setAdDistribution = function(adDistribution) {
        self.columns.append(adDistribution);
    }
    return rst;
};


ReportRequest.prototype.getAggregation = function() {
    return this.aggregation;
};

ReportRequest.prototype.getReturnOnlyCompleteData = function() {
    return this.returnOnlyCompleteData;
};

ReportRequest.prototype.getReportName = function() {
    return this.reportName;
};

ReportRequest.prototype.getLanguage = function() {
    return this.reportLanguage;
};
ReportRequest.prototype.getFormat = function() {
    return self.reportFormat;
};

ReportRequest.prototype.createKeywordPerformanceReportRequest = function() {
    var rst = {};
    var self = this;
    this.reportType = 'keywordPerformanceReportRequest';
    this.keywordPerformanceReportRequest = ET.Element('ReportRequest');
    this.keywordPerformanceReportRequest.set('xsi:type', 'KeywordPerformanceReportRequest');
    this.submitGenerateReportRequest.append(this.keywordPerformanceReportRequest);

    rst.target = this.keywordPerformanceReportRequest;
    rst.setFormat = function() {
        self[self.reportType].append(self.reportFormat);
    };
    rst.setLanguage = function() {
        self[self.reportType].append(self.reportLanguage);
    };
    rst.setReportName = function() {
        self[self.reportType].append(self.reportName);
    };
    rst.setReturnOnlyCompleteData = function(returnOnlyCompleteData) {

        self[self.reportType].append(returnOnlyCompleteData);

    };
    rst.setAggregation = function(aggregation) {
        self.aggregation = ET.Element('Aggregation');
        self.aggregation.text = aggregation;
        self[self.reportType].append(self.aggregation);
    };
    rst.setColumns = function(columns) {
        self[self.reportType].append(columns);
    };
    rst.setScope = function(scope) {
        self[self.reportType].append(scope);
    };
    rst.setTime = function(time) {
        self[self.reportType].append(time);
    };
    return rst;
};
ReportRequest.prototype.createReportFormat = function(format) { //Xml/Csv/Tsv
    if('' === format) {
        throw new Error('missing required paramater format');
    }
    var rst = {};
    this.reportFormat = ET.Element('Format');
    this.reportFormat.text = format;
    rst.target = this.reportFormat;
    return rst;
};
ReportRequest.prototype.createReportLanguage = function(language) { //Englist/French
    if('' === language) {
        throw new Error('missing required paramater language');
    }
    var rst = {};
    this.reportLanguage = ET.Element('Language');
    this.reportLanguage.text = language;
    rst.target = this.reportLanguage;
    return rst;
};
ReportRequest.prototype.createReportName = function(reportName) { //Englist/French
    if('' === reportName) {
        throw new Error('missing required paramater reportName');
    }
    var rst = {};
    this.reportName = ET.Element('ReportName');
    this.reportName.text = reportName;
    rst.target = this.reportName;
    return rst;
};
ReportRequest.prototype.createReturnOnlyCompleteData = function(returnOnlyCompleteData) { //Boolean
    var rst = {};
    this.returnOnlyCompleteData = ET.Element('ReturnOnlyCompleteData');
    if('' === returnOnlyCompleteData) {
        throw new Error('missing required paramater returnOnlyCompleteData');
    } else if(false === returnOnlyCompleteData) {
        this.returnOnlyCompleteData.set('xsi:nil', false);
    } else if(true === returnOnlyCompleteData) {
        this.returnOnlyCompleteData.set('xsi:nil', true); 
    }
    this.returnOnlyCompleteData.text = returnOnlyCompleteData.toString();
    rst.target = this.returnOnlyCompleteData;
    return rst;
};

ReportRequest.prototype.setApplicationToken = function(applicationToken) {
    this.applicationToken = ET.SubElement(this.header, 'ApplicationToken');
    this.applicationToken.text = applicationToken;
};
ReportRequest.prototype.getApplicationToken = function() {
    return this.applicationToken;
};
ReportRequest.prototype.setCustomerAccountId = function(customerAccountId) {
    this.customerAccountId = ET.SubElement(this.header, 'CustomerAccountId');
    this.customerAccountId.text = customerAccountId;
};
ReportRequest.prototype.getCustomerAccountId = function() {
    return this.customerAccountId;
};
ReportRequest.prototype.setCustomerId = function(customerId) {
    this.customerId = ET.SubElement(this.header, 'CustomerId');
    this.customerId.text = customerId;
};
ReportRequest.prototype.getCustomerId = function() {
    return this.customerId;
};
ReportRequest.prototype.setDeveloperToken = function(developerToken) {
    this.developerToken = ET.SubElement(this.header, 'DeveloperToken');
    this.developerToken.text = developerToken;
};
ReportRequest.prototype.getDeveloperToken = function() {
    return this.developerToken;
};
ReportRequest.prototype.setPassword = function(password) {
    this.password = ET.SubElement(this.header, 'Password');
    this.password.text = password;
};
ReportRequest.prototype.getPassword = function() {
    return this.password;
};
ReportRequest.prototype.setUserName = function(userName) {
    this.userName = ET.SubElement(this.header, 'UserName');
    this.userName.text = userName;
};
ReportRequest.prototype.getUserName = function() {
    return this.userName;
};



ReportRequest.prototype.toXMLString = function() {
    // console.log('toXMLString');
    return toXMLString.call(this.root);
};

function toXMLString() {
    return ET.tostring(this, {
        xml_declaration: false
    });
}
exports.ReportRequest = ReportRequest;
exports.PollReportRequest = PollReportRequest;